<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 13. 求一组数中的最大值和最小值，以及所在位置的下标。

    function maxAndMin (arr) {
      if (!(arr instanceof Array) || arr.length === 0) throw new Error('请传入一个有值的数组')
      var max, min
      max = min = arr[0]
      for (var i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
          max = arr[i]
          continue
        }
        if (arr[i] < min) min = arr[i]
      }
      return { max: max, min: min }
    }
    console.log(maxAndMin([321, 543, 23, 76, 123, 87, 12, 98]))
  </script>
</body>
</html>